package com.zeng.vueserve.coreservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.vueserve.coreservice.mapper.RoleAuthorityMapper;
import com.zeng.vueserve.coreservice.entity.RoleAuthority;
import com.zeng.vueserve.coreapi.vo.RoleAuthorityVo;
import com.zeng.vueserve.coreapi.service.RoleAuthorityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleAuthorityServiceImpl implements RoleAuthorityService {

    @Autowired
    RoleAuthorityMapper roleAuthorityMapper;

    @Override
    public RoleAuthorityVo addOne(RoleAuthorityVo roleAuthorityVo) {
    RoleAuthority roleAuthority = RoleAuthority.builder().build();
    BeanUtils.copyProperties(roleAuthorityVo, roleAuthority, IgnoreStringUtil.ignoreExclude());
    int insert = roleAuthorityMapper.insert(roleAuthority);
    return BeanUtil.beanToVo(roleAuthority, roleAuthorityVo);
    }

    @Override
    public RoleAuthorityVo getOneByCode(String code) {

    RoleAuthority roleAuthority = this.roleAuthorityMapper.selectById(code);
    RoleAuthorityVo roleAuthorityVo = BeanUtil.beanToVo(roleAuthority, new RoleAuthorityVo());

    return roleAuthorityVo;
    }

    @Override
    public Integer update(RoleAuthorityVo roleAuthorityVo) {
    RoleAuthority roleAuthority = BeanUtil.VoToBean(roleAuthorityVo,
    new RoleAuthority(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.roleAuthorityMapper.updateById(roleAuthority);
    }

    @Override
    public Integer delete(String code) {
    return this.roleAuthorityMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.roleAuthorityMapper.deleteBatchIds(codes);
    }

    @Override
    public List<RoleAuthorityVo> findPage(Page page, RoleAuthorityVo roleAuthorityVo) {
        RoleAuthority roleAuthority = BeanUtil.VoToBean(roleAuthorityVo, new RoleAuthority());
        this.roleAuthorityMapper.selectPage(page, this.getWrapper(roleAuthorityVo));
        List orders = page.getRecords();
        List<RoleAuthorityVo> roleAuthorityVos = new ArrayList<>();
        orders.forEach(e->{
            roleAuthorityVos.add(BeanUtil.beanToVo(e, new RoleAuthorityVo()));
        });
        page.setRecords(null);
        return roleAuthorityVos;
        }


    @Override
    public Integer count(RoleAuthorityVo roleAuthorityVo) {
        Integer integer = this.roleAuthorityMapper.selectCount(this.getWrapper(roleAuthorityVo));
        return integer;
    }

    public AbstractWrapper getWrapper(RoleAuthorityVo roleAuthorityVo) {
        QueryWrapper<RoleAuthority> wrapper = new QueryWrapper<>();
        if (roleAuthorityVo != null) {


        }
        return wrapper;
    }
}
